package com.new1cloud.file.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class ThreadPoolConfig {
    // 核心线程池大小
    @Value("${upload.threadpool.coresize:4}")
    private int corePoolSize;

    // 最大可创建的线程数
    @Value("${upload.threadpool.maxsize:10}")
    private int maxPoolSize;

    // 队列最大长度
    @Value("${upload.threadpool.queuesize:1000}")
    private int queueCapacity;

    // 线程池维护线程所允许的空闲时间
    @Value("${upload.threadpool.aliveseconds:0}")
    private int keepAliveSeconds;

    @Bean(name = "myExecutorService")
    @ConditionalOnMissingBean(name = "myExecutorService")
    public ExecutorService threadPoolTaskExecutor()
    {
        return new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveSeconds,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(queueCapacity));
    }
}
